<!--
*@Author: QWP
*@Description: 业务短信通知配置
*@Date: 2024-09-12 14:49:22
-->
<template>
  <a-layout class="f-h-100 s-bg-white" id="test">
    <u-table-header>
      <a-row justify="end" class="f-margin-b12">
        <u-button-space>
          <u-icon-button itype="add" @click="toEdit(1)">新增</u-icon-button>
          <!-- <u-icon-button itype="edit" @click="toEdit(2)">修改</u-icon-button> -->
          <u-icon-button itype="delete" @click="deleteBtn">删除</u-icon-button>
        </u-button-space>
      </a-row>
    </u-table-header>
    <u-table-content>
      <u-table
        :columns="columns"
        :data="tabData"
        v-model:pagination="pagenation"
        :loading="loading"
        :scroll="{ y: useTableScroll() }"
        @onSelect="({ selectedRows }) => tabSelData = selectedRows"
        @change="getSearch">
      </u-table>
    </u-table-content>
  </a-layout>

  <!-- 编辑弹窗 -->
  <Edit v-model:open="showEdit"
    title="短信发送配置" 
    @success="showEdit = false; getSearch()"/>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted, h} from "vue";
import { _IFormEl } from "@/components/common/form/interface";
import { TableColumnProps, Popconfirm, Switch } from "ant-design-vue";
import { alertC, nullC  } from '@/util/common'
import Edit from './Edit.vue'
import { messageC } from "@/util/common";
import { useTableScroll } from "@/hooks/useTable";
import * as smsCApi from '@/service/system/smsConfig/index.ts'
import { busiTypes } from './datas'

onMounted(() => {
  getSearch()
})

let pagenation = reactive({
  total: 0,
  current: 1,
  pageSize: 50,
  showTotal: total => `共 ${total} 条`
})

// 列表
const loading = ref<boolean>(false)
const tabData = ref<any[]>([]);
const tabSelData = ref<any[]>([]);
let columns: Array<TableColumnProps> = reactive([
  { dataIndex: "index", title: "序号", width: 50, align: 'center',
    customRender: ({ index }) => index += 1
  },
  { dataIndex: "moudletype", title: "业务名称", minWidth: 150, 
    customRender: ({ text }) => {
      let info = busiTypes.find(i => i.value == text) || {}
      return nullC(info['label'])
    }
  },
  { dataIndex: "isenable", title: "是否启用短信", minWidth: 150, align: 'center',
    customRender: ({ text, record, index }) => {
      return h(Popconfirm, { 
        title: `确认${text == 1 ? '停用' : '启用'}?`, 
        onConfirm: () => updateSubmit(record),}, 
        () => [
          h(Switch, { checked: text == 1, checkedChildren: '启用', unCheckedChildren: '停用' })
        ]
      )
    }
  },
]);

// 列表查询
const getSearch = async (current: number) => {
  loading.value = true
  try {
    if(current == 1) {
      pagenation.current = 1;
    }
    const res = await smsCApi.page({ size: pagenation.pageSize, current: pagenation.current, model: {keywords: ''} });
    if(res.code == 0) {
      tabData.value = res.data.records || [];
      pagenation.total = res.data.total || 0
    }
  } catch (error) {}
  loading.value = false;
}

// 编辑
const currntRow = ref({});
let dialogTitle = ref<string>("新建发文")
let isCommon = ref<boolean>(false)
let showEdit = ref<boolean>(false)
let showFlow = ref<boolean>(false)
// type 1 新增     2 修改
const toEdit = (type, isCommon) => {
  if(type == 2) {
    if(tabSelData.value.length != 1 ) {
      alertC("请选择一条数据进行编辑!");
      return
    }
    if(isCommon == true) {
      isCommon.value = true
    }
    currntRow.value = tabSelData.value[0];
  }

  showEdit.value = true
}

// 修改启用状态
const updateSubmit = async(record) => {
  loading.value = true;
  try {
    const res = await smsCApi.update({
      isenable: record['isenable'] == '1' ? 2 : 1,
      keyid: record['keyid'],
      moudletype: record['moudletype'],
    })
    if(res.code == 0) {
      messageC(res.msg, "success")
      getSearch()
    }
    else {
      messageC(res.msg, "error")
    }
  } catch (error) {}
  loading.value = false;
}

// 删除
const deleteBtn = () => {
  if (tabSelData.value.length == 0) {
    alertC("请先选择要删除的数据!", "warning");
    return;
  }
  let ids = tabSelData.value.map((item) => item.keyid);
  alertC("确定删除选中的数据?", 'confirm').then(async () => {
    try {
      loading.value = true;
      const data = await dispatchApi.del({ longs: ids.join() });
      if (data.code == 0) {
        messageC(data.msg, "success");
        getSearch();
      } else {
        alertC(data.msg, "warning");
      }
      loading.value = false;
    } catch (error) {
      loading.value = false;
    }
  })
}

</script>

<style lang="scss" scoped>
.icons-list :deep(.anticon) {
  margin-right: 6px;
  font-size: 24px;
}
</style>
